package study.saas.order.service;

import org.springframework.stereotype.Service;
import study.saas.common.entity.YPageable;
import study.saas.common.repo.Repo;
import study.saas.common.util.CopyClassUtil;
import study.saas.common.util.PageUtils;
import study.saas.common.util.StringUtil;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.common.viewmodel.PageModel;
import study.saas.order.entity.Commodity;
import study.saas.order.entity.Employee;
import study.saas.order.repo.CommodityRepo;
import study.saas.order.service.model.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zxx
 */
@Service
public class CommodityService {
    @Resource
    private CommodityRepo commodityRepo;

    public ApiResultV2<Commodity> get(String commodityName) {
        Commodity commodity = commodityRepo.findFirstBycommodityName(commodityName);
        if (commodity !=null && commodity.isDelete()) {
             commodity=null;
        }
        return ApiResultV2.ok(commodity);
    }

    public ApiResultV2 add(CommodityAddDTO add) {
        Commodity commodity = commodityRepo.findFirstBycommodityName(add.getCommodityName());
        if (commodity != null && !commodity.isDelete()) {
            return ApiResultV2.error("商品名称重复");
        }
        Commodity newCommodity;
        if (commodity==null) {
            newCommodity = new Commodity();
        }else{
            newCommodity=commodity;
            newCommodity.setIsDeleted(0);
        }
        CopyClassUtil.copyClass(add,newCommodity);
        commodityRepo.save(newCommodity);
        return ApiResultV2.ok();
    }

    public ApiResultV2 modify(CommodityModifyDTO modify) {
        Commodity commodity = commodityRepo.findFirstBycommodityName(modify.getCommodityName());
        if (commodity == null || commodity.isDelete()) {
            return ApiResultV2.error("商品不存在");
        }
        CopyClassUtil.copyClass(modify, commodity);
        commodityRepo.save(commodity);
        return ApiResultV2.ok();
    }

    public ApiResultV2 delete(String commodityName) {
        Commodity commodity = commodityRepo.findFirstBycommodityName(commodityName);
        if (commodity == null || commodity.isDelete()) {
            return ApiResultV2.ok();
        }
        commodity.setIsDeleted(1);
        commodityRepo.save(commodity);
        return ApiResultV2.ok();
    }

    public ApiResultV2 remove(String commodityName) {
        Commodity commodity = commodityRepo.findFirstBycommodityName(commodityName);
        if (commodity == null) {
            return ApiResultV2.error("该商品不存在");
        }
        commodityRepo.delete(commodity);
        return ApiResultV2.ok();
    }

    public ApiResultV2<PageModel<Commodity>> list(CommodityFilterDTO filter) {
        YPageable pageable = PageUtils.Ypageable(filter.getPage(), filter.getPageSize());
        StringBuilder sql = new StringBuilder();
        Map<String, Object> param = new HashMap<>();
        sql.append(" select c from Commodity c where c.isDeleted = 0 ");
        if (!StringUtil.isEmpty(filter.getCommodityNameEq())) {
            sql.append(" and c.commodityName = :getCommodityNameEq");
            param.put("getNameEq", filter.getCommodityNameEq());
        }
        if (!StringUtil.isEmpty(filter.getCommodityNameLike())) {
            sql.append(" and c.commodityName like :getCommodityNameLike");
            param.put("getCommodityNameLike", "%" + filter.getCommodityNameLike() + "%");
        }
        sql.append(" order by c.createTime desc,amount desc,price desc ");
        pageable.setCountSql(Repo.toCountJPql(sql.toString()));
        PageModel<Commodity> pageModel = commodityRepo.queryPage(sql.toString(), param,Commodity.class,pageable);
        return ApiResultV2.ok(pageModel);
    }

    public ApiResultV2 up(String commodityName) {
        Commodity commodity = commodityRepo.findFirstBycommodityName(commodityName);
        if (commodity == null ) {
            return ApiResultV2.error("该商品不存在");
        }
        commodity.setIsDeleted(0);
        commodityRepo.save(commodity);
        return ApiResultV2.ok();
    }

    public ApiResultV2 decrease(CommodityDecreaseDTO decrease) {
        Commodity commodity = commodityRepo.findFirstBycommodityName(decrease.getCommodityName());
        if (commodity == null || commodity.isDelete()) {
            return ApiResultV2.error("商品不存在");
        }
        if (decrease.getDecreaseAmount()>commodity.getAmount()) {
            return ApiResultV2.error("库存不足");
        }
        Integer newAmount = commodity.getAmount()-decrease.getDecreaseAmount();
        commodity.setAmount(newAmount);
        commodityRepo.save(commodity);
        return ApiResultV2.ok();
    }

    public ApiResultV2 increase(CommodityIncreaseDTO increase) {
        Commodity commodity = commodityRepo.findFirstBycommodityName(increase.getCommodityName());
        if (commodity == null || commodity.isDelete()) {
            return ApiResultV2.error("商品不存在");
        }
        Integer newAmount = commodity.getAmount()+increase.getIncreaseAmount();
        commodity.setAmount(newAmount);
        commodityRepo.save(commodity);
        return ApiResultV2.ok();
    }
}
